package com.pb.poem.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import com.pb.poem.util.ConfigManager;

//基类：数据库通用操作类
public class BaseDao {
protected Connection conn;
protected PreparedStatement ps;
protected Statement stmt;
protected ResultSet rs;
//获得数据库连接
public boolean getConnection(){
	String driver=ConfigManager.getInstance().getString("jdbc.driver_class");
	String url=ConfigManager.getInstance().getString("jdbc.connection.url");
	String username=ConfigManager.getInstance().getString("jdbc.connection.username");
	String password=ConfigManager.getInstance().getString("jdbc.connection.password");	
	try {
		Class.forName(driver);
		conn=DriverManager.getConnection(url,username,password);
	} catch (ClassNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	}
	return true;
}

//增删改delete from news_detail where id=4 ?and title="李晓娜"?
public int executeupdate(String sql,Object[] params){
	
	int updateRows=0;
	getConnection();
	try {
		ps=conn.prepareStatement(sql);
		//填充占位符,循环数组
		
		for(int i=0;i<params.length;i++){
			ps.setObject(i+1,params[i]);
		}
		updateRows=ps.executeUpdate();
	} catch (SQLException e) {
		
		e.printStackTrace();
	}
	return updateRows;
}
//查询一个返回一个结果
public int insertReturnId(String sql,Object[] params){
	executeupdate(sql,params);
	String sql2="SELECT LAST_INSERT_ID() as pid;";
	try {
		ps.close();
		ps=conn.prepareStatement(sql2);
		rs = ps.executeQuery();
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		if(rs.next()){
			System.out.println("///"+rs.getInt("pid"));
			return rs.getInt("pid");
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return 0;
	
}

//查询
public ResultSet executeSQL(String sql,Object[] params){
	getConnection();
	try {
		ps=conn.prepareStatement(sql);
		for(int i=0;i<params.length;i++){
		ps.setObject(i+1,params[i]);
		}
		rs=ps.executeQuery();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return rs;
}
//关闭资源
public boolean closeResource(){
	if(rs!=null){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	if(ps!=null){
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	if(stmt!=null){
		try {
			stmt.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	if(conn!=null){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	return true;
	
}
}
